/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or withouta
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of sourcec code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following discalimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their name, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ===================================================tre=================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*
*  $Id: HTMLNormalizerFilter.java,v 1.4 2007/05/23 21:00:17 nano Exp $
*/
package net.jxta.myjxta.dialog.filter;

import net.jxta.logging.Logging;
import net.jxta.myjxta.dialog.DialogMessage;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Normalizes HTML Text to be displayed in the Dialog panel.
 *
 * @author james todd [gonzo at jxta dot org]
 * @author mike mcangus [mcangus at jxta dot org]
 */
public final class HTMLNormalizerFilter extends AbstractDialogFilter {

    private static final String IMPLIED_SUFFIX = "-implied";
    private static final String HTML_PREFIX = "<html";

    private static final Logger LOG = Logger.getLogger(HTMLNormalizerFilter.class.getName());

    private static final String PREFIX = "<span class='preamble'>[";
    private static final String AT = "@";
    private static final String POSTFIX = "]</span>";
    private static final String SPACE = " "; //"&nbsp;";

    private static final String PREAMBLE_FORMAT = PREFIX + "<span class='peerName'>{0}</span>" + AT + "{1}" + POSTFIX + SPACE;
    private static final String TIMESTAMP_FORMAT = "hh:mm:ss";

    private final MessageFormat preambleFormatter = new MessageFormat(PREAMBLE_FORMAT);
    private final SimpleDateFormat dateFormatter = new SimpleDateFormat(TIMESTAMP_FORMAT);

    /**
     * Called if a new DialogMessage was received
     *
     * @param msg the newly received DialogMessage object
     * @return the newly modified DialogMessage object.
     */
    public DialogMessage filter(DialogMessage msg) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin filter(DialogMessage)");
            LOG.fine("msg      = " + msg.getOriginator() + ":" +
                    msg.getHtmlMessage());
        }

        String text = null;
        String preamble = null;
        String htmlText = null;

        if (msg != null) {
            text = msg.getHtmlMessage() != null ?
                    msg.getHtmlMessage().trim() : "";
            String orig = msg.getOriginator();
            Date timeStamp = msg.getTimeStamp();
            preamble = this.preambleFormatter.format(new Object[]{
                    orig != null && orig.trim().length() > 0 ?
                            orig : DialogMessage.DEFAULT_ORIGINATOR,
                    this.dateFormatter.format(timeStamp != null ? timeStamp : new Date())
            });
        }

        HTMLEditorKit htmlEditor = new HTMLEditorKit();
        HTMLDocument workingDoc = (HTMLDocument) htmlEditor.createDefaultDocument();
        Element bodyTag = null;
        Element pTag = null;

        if (text.toLowerCase().indexOf(HTML_PREFIX) > -1) {
            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
                LOG.fine("Processing text/html document");
            }

            // Put the HTML msg into the workingDoc, without the preamble.
            try {
                htmlEditor.read(new StringReader(text), workingDoc, 0);

                // Add the preamble to the message
                bodyTag = getFirstElement(workingDoc, HTML.Tag.BODY);
                pTag = getFirstElement(bodyTag, HTML.Tag.P);
                workingDoc.insertAfterStart(pTag, preamble);
            } catch (IOException ioe) {
                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                    LOG.log(Level.SEVERE, "Caught unexpected Exception", ioe);
                }

                return msg;
            } catch (BadLocationException ble) {
                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                    LOG.log(Level.SEVERE, "Caught unexpected Exception", ble);
                }

                return msg;
            }

            htmlText = getHTML(workingDoc);

            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE) && preamble.length() > 0) {
                LOG.fine("message = " + htmlText);
            }
        } else if (text.length() > 0) {
            if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
                LOG.fine("Processing text/plain document");
            }

            bodyTag = getFirstElement(workingDoc, HTML.Tag.BODY);
            pTag = getFirstElement(bodyTag, HTML.Tag.P);

            try {
                if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
                    LOG.fine("preamble:msg = " + preamble + ":" + text);
                }

                workingDoc.insertAfterStart(pTag, preamble + text);
            } catch (IOException ioe) {
                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                    LOG.log(Level.SEVERE, "Caught unexpected Exception", ioe);
                }

                return msg;
            } catch (BadLocationException ble) {
                if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                    LOG.log(Level.SEVERE, "Caught unexpected Exception", ble);
                }

                return msg;
            }

            htmlText = getHTML(workingDoc);
        } else {
            htmlText = "";
        }

        msg.setHtmlMessage(htmlText);

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("returning \"" + msg + "\"");
            LOG.fine("End   normalize(String, String)");
        }

        return msg;
    }

    private static Element getFirstElement(HTMLDocument d, HTML.Tag t) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("In getFirstElement(HTMLDocument, HTML.Tag)");
        }

        return getFirstElement(d.getDefaultRootElement(), t);
    }

    private static Element getFirstElement(Element e, HTML.Tag t) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin getFirstElement(Element, HTML.Tag)");
            LOG.fine("Element = " + e.getName());
            LOG.fine("HTML.Tag = " + t.toString());
        }

        Element te = null;
        Element be = null;

        for (int i = 0; i < e.getElementCount(); i++) {
            be = e.getElement(i);

            if (be.getName().equals(t.toString()) ||
                    be.getName().equals(t.toString() + IMPLIED_SUFFIX)) {
                te = be;
                break;
            }

            if (!be.isLeaf()) {
                te = getFirstElement(be, t);
            }
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("returning " + (te != null ? te.getName()
                    : "null"));
            LOG.fine("End   getFirstElement(Element, HTML.Tag)");
        }

        return te;
    }

    private static String getHTML(Document d) {
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Begin getHTML(Document)");
        }

        Writer w = new StringWriter();
        HTMLEditorKit ek = new HTMLEditorKit();

        try {
            ek.write(w, d, 0, d.getLength());
        } catch (IOException ioe) {
            if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                LOG.log(Level.SEVERE, "Caught unexpected Exception", ioe);
            }
        } catch (BadLocationException ble) {
            if (Logging.SHOW_SEVERE && LOG.isLoggable(Level.SEVERE)) {
                LOG.log(Level.SEVERE, "Caught unexpected Exception", ble);
            }
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("End   getHTML(Document)");
        }

        return w != null ? w.toString() : null;
    }
}
